# -*- coding: utf-8 -*-
from flask import Blueprint,render_template,g,request,jsonify
from models.pay.PayOrder import PayOrder
from models.pay.PayOrderDetail import PayOrderDetail
from models.food.Food import Food
from models.member.Member import Member
from Service import Pagination
from app import db

route_finance = Blueprint('finance_page',__name__ )

@route_finance.route("/index")
def index():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    # 搜索
    status = int(request.args.get("status", -1))
    # 保存搜索状态
    condition = {
        "status": status,
    }

    query = PayOrder.query
    if status != -1:
        query = query.filter_by(status=status)

    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(PayOrder.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    order_list = []
    for item in data_list:
        order_info = {}
        order_info["id"] = item.id
        order_info["order_sn"] = item.order_sn
        order_info["status"] = item.status
        order_info["pay_price"] = item.pay_price
        order_info["pay_time"] = item.pay_time
        order_info["created_time"] = item.created_time
        order_info_detail = PayOrderDetail.query.filter_by(pay_order_id=item.id).all()
        lis = []
        for k in order_info_detail:
            food = {}
            food["food_quantity"] = k.quantity
            food["food_name"] = Food.query.filter_by(id=k.food_id).first().name
            lis.append(food)

        order_info["foods"] = lis

        order_list.append(order_info)
    pagination["order_list"] = order_list
    return render_template("finance/index.html",current_user=current_user,pagination=pagination,condition=condition)

@route_finance.route("/change",methods=["POST"])
def change():
    req_dic = request.values
    order = PayOrder.query.get(req_dic["id"])
    try:
        order.status = req_dic["change"]
        db.session.add(order)
        db.session.commit()
    except:
        return jsonify({"code":-1,"msg":"操作失败！"})

    return jsonify({"code": 200, "msg": "操作成功！"})

@route_finance.route("/pay-info")
def payInfo():
    current_user = g.current_user
    id = request.args.get("id")
    order_info = PayOrder.query.get(id)
    order_detail = PayOrderDetail.query.filter_by(pay_order_id=id).all()
    lis = []
    for item in order_detail:
        food = {}
        food["quantity"] = item.quantity
        f = Food.query.filter_by(id=item.food_id).first()
        food["name"] = f.name
        food["price"] = item.price
        lis.append(food)

    data = {
        "order_info": order_info,
        "food_info": lis,
        "member_name": Member.query.get(order_info.member_id).nickname
    }
    return render_template("finance/pay_info.html",current_user=current_user,data=data)


@route_finance.route("/account")
def account():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))
    sum_price = db.session.execute("select sum(pay_price-yun_price) from pay_order where status = 1").first()[0]
    query = PayOrder.query
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(PayOrder.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list
    pagination["sum_price"] = sum_price
    return render_template("finance/account.html",current_user=current_user,pagination=pagination)
